let data = {
    name: '谭',
    age: '18'
}


function getName() {
    let nameDom = document.querySelector('.name')
    nameDom.textContent = data.name
}
function getAge() {
    let nameDom = document.querySelector('.age')
    nameDom.textContent = data.age
}


function observe(data) {
    for (const key in data) {
        let internalValue = data[key]
        let listFun = []
        Object.defineProperty(data, key, {
            get() {
                console.log('读取了')
                //记录哪些函数调用了
                if(window.__func&&!listFun.includes(window.__func)){
                    listFun.push(window.__func)
                }
                // console.log(listFun)
                // listFun.add(window.__func)
                return internalValue
            },
            set(val) {
                console.log(key, '更改了')
                internalValue = val
                for (let fun of listFun) {
                    console.log(fun)
                    fun()
                }
                // for(i=0;i<listFun.length;i++){
                //     listFun[i]()
                // }
            }
        })
    }
}

function autoRun(func){
    window.__func = func
    func()
    window.__func = null
}
observe(data)

autoRun(getName)
autoRun(getAge)



